/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ig.ejb.metrica.da.editor.dao;

import ig.ejb.metrica.da.editor.dao.abs.DAO;
import ig.ejb.metrica.da.session.editor.dto.ColumnaOriegenDatoDTO;
import java.util.List;

/**
 *
 * @author jsanchez
 */
public class ColumnaOrienDatoDao extends DAO {

   @Override
    public Object buscar(Integer id) throws Exception {
        try {
            getConnection();
            prep = connection.prepareStatement("SELECT ID_ODATOS, ID_COLUMNA, COLUMNA_LABEL, COLUMNA_VALOR FROM COLUMNA_ODATOS WHERE ID_COLUMNA = ?");
            prep.setInt(1, id.intValue());
            resul = prep.executeQuery();
            Object dto = null;
            if (resul.next()) {
                dto = new ColumnaOriegenDatoDTO();
                getDatos(resul, dto);
            }
            return dto;
        } catch (ClassNotFoundException ex) {
            throw new Exception(ex);
        } catch (java.sql.SQLException ex) {
            throw new Exception(ex);
        } catch (Exception ex) {
            throw new Exception(ex);
        } finally {
            closeAll();
        }
    }


    @Override
    public List buscarTodos() throws Exception {
        List c = new java.util.ArrayList();
        try {
            getConnection();
            prep = connection.prepareStatement("SELECT ID_ODATOS, ID_COLUMNA, COLUMNA_LABEL, COLUMNA_VALOR FROM COLUMNA_ODATOS");
            resul = prep.executeQuery();
            while (resul.next()) {
                c.add(getDatos(resul, new ColumnaOriegenDatoDTO()));
            }
            return c;
        } catch (ClassNotFoundException ex) {
            throw new Exception(ex);
        } catch (java.sql.SQLException ex) {
            throw new Exception(ex);
        } catch (Exception ex) {
            throw new Exception(ex);
        } finally {
            closeAll();
        }
    }

    @Override
    public Integer guardar(Object obj) throws Exception {
        ColumnaOriegenDatoDTO dto = (ColumnaOriegenDatoDTO) obj;
        try {            
            if(!(Boolean)existe(obj)){
                getConnection();
                prep = connection.prepareStatement("INSERT INTO COLUMNA_ODATOS(COLUMNA_LABEL, COLUMNA_VALOR, ID_ODATOS, ID_COLUMNA) VALUES(?, ?, ?, ?)");
            } else {
                getConnection();
                prep = connection.prepareStatement("UPDATE COLUMNA_ODATOS SET COLUMNA_LABEL=?, COLUMNA_VALOR=?, ID_ODATOS=? WHERE ID_COLUMNA=? ");
            }
            setDatos(prep, dto,"COLUMNA_LABEL, COLUMNA_VALOR, ID_ODATOS, ID_COLUMNA");
            prep.executeUpdate();
            return dto.getId_Columna();
        } catch (ClassNotFoundException ex) {
            throw new Exception(ex);
        } catch (java.sql.SQLException ex) {
            throw new Exception(ex);
        } catch (Exception ex) {
            throw new Exception(ex);
        } catch (Throwable ex) {
            throw new Exception(ex);
        } finally {
            closeAll();
        }
    }

    @Override
    public void eliminar(Integer id) throws Exception {
        try {
            getConnection();
            prep = connection.prepareStatement("DELETE FROM COLUMNA_ODATOS WHERE ID_COLUMNA = ?");
            prep.setInt(1, id.intValue());
            prep.executeUpdate();
        } catch (java.sql.SQLException ex) {
            throw new Exception(ex);
        } catch (Exception ex) {
            throw new Exception(ex);
        } finally {
            closeAll();
        }
    }

    @Override
    public Object existe(Object obj) throws Exception {
        try {
            getConnection();
            prep = connection.prepareStatement("SELECT ID_ODATOS, ID_COLUMNA, COLUMNA_LABEL, COLUMNA_VALOR FROM COLUMNA_ODATOS WHERE ID_COLUMNA= ?");
            //prep.setInt(1, ((ColumnaOriegenDatoDTO) obj).getId_ODatos());
            prep.setInt(1, ((ColumnaOriegenDatoDTO) obj).getId_Columna());
            resul = prep.executeQuery();
            return resul.next();
        } catch (java.sql.SQLException ex) {
            throw new Exception(ex);
        } catch (Exception ex) {
            throw new Exception(ex);
        } finally {
            closeAll();
        }        
    }

}